﻿package net.wangit.adm.org;

import net.wangit.context.ActionContext;
import net.wangit.jdbc.ColumnRow;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.Table;
import net.wangit.support.action.TransactionAction;
import net.wangit.user.admin.AdminCache;
import net.wangit.user.org.OrgCache;
import net.wangit.util.Checker;

public class OrgAuthAction extends TransactionAction {

	private static final String SQL_CLEAR = "delete from AORGROLE where orgid=?";

	public OrgAuthAction() {
	}

	public void doAction(DB db, ActionContext ac) throws Exception {
		String oid = ac.getRequestParameter("org.roles.orgid");
		if (Checker.isEmpty(oid))
			return;
		db.execute("delete from AORGROLE where orgid=?", oid, 2);
		String checks[] = ac.getRequestParameters("_treecheck");
		if (!Checker.isEmpty(checks)) {
			Table table = new Table("adm", "AORGROLE");
			for (int i = 0; i < checks.length; i++) {
				ColumnRow row = table.addInsertRow();
				row.set("orgid", oid);
				row.set("roleid", checks[i]);
			}

			db.save(table);
		}
		OrgCache.clear(oid);
		AdminCache.clearAll();
	}
}
